//
//  Account.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/2/7.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import ObjectMapper

enum AccountNotification: String{
    case login = "AccountNotification.login"
    case logout = "AccountNotification.logout"
    case tokenInvalid = "AccountNotification.tokenInvalid"
    case rigist = "AccountNotification.gigist"
}


class AccountService {
   
    public static var `default`: AccountService { return AccountService() }
  
    
    ///退出登录
    func logout() {
        AccountStorage.default.removeAccount()
        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: AccountNotification.logout.rawValue), object: nil)
    }
    
    ///授权失效
    func tokenInvalid() {
        AccountStorage.default.removeAccount()
        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: AccountNotification.tokenInvalid.rawValue), object: nil)
    }
}

// MARK:-
// MARK: Regist
extension AccountService {
    
    //    登录
    static func login(_ mobile:String,
                      password:String,
                      success:((AccountModel) -> Void)?,
                      failure:NetworkFailure?) {
        let path = "login"
        let para = ["mobile": mobile,
                    "password": password]
        NetworkService.post(path, parameter: para, success: { (response, data) in
            
            guard let data = data as? [String: Any] else {  failure?(-1, "nil data"); return }
            guard let account = Mapper<AccountModel>().map(JSON: data)  else {  failure?(-1, "nil data"); return }
            
            AccountStorage.default.mobile = mobile
            AccountStorage.default.password = password
            //保存账号信息
            AccountStorage.default.updateAccount(account)

            NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: AccountNotification.login.rawValue), object: nil)
            success?(account)
        }, failure: failure)
    }
    
    ///退出登录
    static func logout(success:NetworkSuccess?,
                       failure:NetworkFailure?) {
        ///删除本地记录
        AccountService.default.logout()
        
        let path = "logout"
        NetworkService.post(path, success: { (response, data) in
            success?(response, data)
        }, failure: failure)
    }
    
    /// 获取验证码
    static func getSmsAuthCode(_ mobile:String,
                               success:( (_ verifyCode:String) -> Void)?,
                               failure:NetworkFailure?) {
        let path = "sms-auth-code"
        let para = ["mobile": mobile]
        NetworkService.post(path, parameter: para, success: { (response, data) in
            
            guard let data = data as? [String: Any] else {  failure?(-1, "nil data"); return }
            guard let code = data["verifyCode"] as? String else { failure?(-1, "nil data"); return }
            success?(code)
        }, failure: failure)
    }
    
    
    /// 校验短信验证码
    ///
    /// - Parameters:
    ///   - mobile: 手机号码
    ///   - verifyCode: 获取短信验证码得到的verifyCode
    ///   - sms: 手机收到的短信验证码
    ///   - success: <#success description#>
    ///   - failure: <#failure description#>
    static func verifySMS(mobile:String,
                          verifyCode:String,
                          sms:String,
                          success:( () -> Void )?,
                          failure:NetworkFailure?) {
        let path = "verify-sms"
        let para = ["mobile": mobile,
                    "verifyCode": verifyCode,
                    "sms": sms]
        NetworkService.post(path, parameter: para, success: { (_, _) in
            success?()
        }, failure: failure)
    }
    
    ///设置密码
    static func setPassword(mobile:String,
                            verifyCode: String,
                            sms: String,
                            password:String,
                            success:NetworkSuccess?,
                            failure:NetworkFailure?) {
        let path = "set-password"
        let para = ["mobile": mobile,
                    "verifyCode": verifyCode,
                    "sms": sms,
                    "password": password]
        NetworkService.post(path, parameter: para, success: { (response, data) in
            guard let data = data as? [String: Any] else {  failure?(-1, "nil data"); return }
            guard let uid = data["uid"] as? String else {  failure?(-1, "nil uid"); return }
            guard let token = data["token"] as? String else {  failure?(-1, "nil token"); return }
            
            AccountStorage.default.password = password
            AccountStorage.default.mobile = mobile
            AccountStorage.default.account.uid = uid
            AccountStorage.default.account.token = token
            AccountStorage.default.updateAccount() //保存账号信息
//            NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: AccountNotification.rigist.rawValue), object: nil)
            
            success?(response, data)
        }, failure: failure)
    }
    
    
    ///修改密码
    static func changePassword(mobile:String,
                               verifyCode: String,
                               sms: String,
                               password:String,
                               success:( () -> Void )?,
                               failure:NetworkFailure?) {
        let path = "change-password"
        let para = ["mobile": mobile,
                    "verifyCode": verifyCode,
                    "sms": sms,
                    "password": password]
        NetworkService.post(path, parameter: para, success: { (_, _) in
            //更新存储密码
            AccountStorage.default.password = password
            success?()
        }) { (code, msg) in
            if code == 1001 {
                AccountService.default.tokenInvalid()
            }
            failure?(code, msg)
        }
    }
    
    ///更新用户信息
    static func updateUserInof(para: [String: Any],
                               success:NetworkSuccess?,
                               failure:NetworkFailure?) {
        let path = "user/update-info"
        NetworkService.post(path, parameter: para, success: success) { (code, msg) in
            if code == 1001 {
                AccountService.default.tokenInvalid()
            }
            failure?(code, msg)
        }
    }
}
